<template>
  <el-dialog :title="info.title" :visible.sync="info.isShow">
    <el-form :model="form">
      <el-form-item label="所属角色" :label-width="formLabelWidth">
        <el-select v-model="form.roleid">
          <el-option label="--请选择--" value></el-option>
          <!-- 遍历菜单列表 -->
          <el-option v-for="item in roles" :key="item.id" :label="item.rolename" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-input v-model="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" :label-width="formLabelWidth">
        <el-input type="password" v-model="form.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-switch
          v-model="form.status"
          active-color="green"
          inactive-color="grey"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button v-if="info.isAdd" type="primary" @click="comfirm">确 定</el-button>
      <el-button v-else type="primary" @click="change()">修 改</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addUser, roleList, oneUser, changeUser } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  data() {
    return {
      form: {
        roleid: "", //角色编号
        username: "", //管理员名称
        password: "",
        status: 1 //1正常  2禁用
      },
      formLabelWidth: "120px",
      roles: []
    };
  },
  methods: {
    comfirm() {
      //添加确定
      addUser(this.form).then(res => {
        // console.log(res);
        if (res.code === 200) {
          //   消息提示
          successAlert(res.msg);
          // 弹框消失
          this.cancel();
          // 触发自定义事件,来更新列表数据
          this.$eventBus.$emit("message");
        }
      });
    },
    cancel() {
      this.info.isShow = false;
      this.form = {
        roleid: "", //角色编号
        username: "", //管理员名称
        password: "",
        status: 1 //1正常  2禁用
      };
    },
    requestRole() {
      //获取roles
      roleList().then(res => {
        if (res.code === 200) {
          this.roles = res.list;
        }
      });
    },
    getoneuser(uid) {
      oneUser({ uid }).then(res => {
        if (res.code === 200) {
          this.form = res.list;
          this.form.uid = res.list.uid;
        }
      });
    },
    change() {
      changeUser(this.form).then(res => {
        if (res.code === 200) {
          successAlert(res.msg);
          this.cancel();
          this.$eventBus.$emit("message");
        }
      });
    }
  },
  mounted() {
    this.requestRole();
  }
};
</script>

<style>
</style>